package com.hoshino.momochi.controller;

import com.hoshino.momochi.model.DTO.FocusDTO;
import com.hoshino.momochi.model.domain.Focus;
import com.hoshino.momochi.model.domain.User;
import com.hoshino.momochi.service.FocusService;
import com.hoshino.momochi.service.UserService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

@RestController
@Slf4j
public class FocusController {

    @Autowired
    private FocusService focusService;

    @Autowired
    private UserService userService;

    private final String USER_ID = CodeEnum.USERID.getMessage();

    /**
     * 新增关注
     *
     * @param request
     * @param focusDTO 需求字段 local_id 被关注的用户id
     * @return
     */
    @RequestMapping("api/focus/add")
    public JsonResponse addFocus(HttpServletRequest request, @RequestBody FocusDTO focusDTO) {
        Long focus_id = (Long) request.getAttribute(USER_ID);
        Long local_id = focusDTO.getLocal_id();
        Focus focus = new Focus();
        focus.setFocusId(focus_id);
        focus.setCreateTime(new Date());
        focus.setLocalId(local_id);
        // 传入值不能错误， 不能重复关注
        if (Objects.isNull(local_id) || Objects.nonNull(focusService.getLocalUserByUserId(focus_id, local_id))) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        focusService.addFocus(focus);
        User user = userService.getFocusAndUserDetails(focus_id);
        return JsonResponse.success(user, CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 取消关注
     *
     * @param request
     * @param focusDTO local_id 被关注者 用户id
     * @return
     */
    @RequestMapping("api/focus/del")
    public JsonResponse delFocus(HttpServletRequest request, @RequestBody FocusDTO focusDTO) {
        Long focus_id = (Long) request.getAttribute(USER_ID);
        Long local_id = focusDTO.getLocal_id();
        Focus focus = new Focus();
        focus.setFocusId(focus_id);
        focus.setLocalId(local_id);
        if (Objects.isNull(local_id)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        focusService.delFocus(focus);
        User user = userService.getFocusAndUserDetails(focus_id);
        return JsonResponse.success(user, CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取 我作为 关注者 被我关注的人的信息
     *
     * @param request
     * @return
     */
    @RequestMapping("api/focus/getLocal")
    public JsonResponse getLocalUser(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Focus focus = new Focus();
        focus.setFocusId(uid);
        return JsonResponse.success(focusService.getLocalUserDetails(focus), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 我作为被关注者 获取粉丝的信息
     *
     * @param request
     * @return
     */
    @RequestMapping("api/focus/getFocus")
    public JsonResponse getFocusUser(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Focus focus = new Focus();
        focus.setLocalId(uid);
        return JsonResponse.success(focusService.getFocusUserDetails(focus), CodeEnum.SUCCESS.getMessage());
    }
}
